package algorithm;




public class leetcode_0108 {
    public TreeNode sortedArrayToBST(int[] nums) {
        int len=nums.length;
        TreeNode head=new TreeNode();
        sorted(nums,0,len-1,head);
        return head;
    }
    public void sorted(int[] num,int left,int right,TreeNode node){
        if(left>right) return;
        int mid=(left+right)/2;
        node.val=num[mid];
        if(mid-1>=left){
            node.left=new TreeNode();
            sorted(num,left,mid-1,node.left);
        }
        if(mid+1<=right){
            node.right=new TreeNode();
            sorted(num,mid+1,right,node.right);
        }
    }
}
 class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }